﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strategy.Core.TradeStreams.Indicators;
using Strategy.Core.TradeTypes;

namespace Strategy.Core.TradeStreams.Indicators
{
    public class EMAIndicatorStream : SMAIndicatorStream
    {
        public EMAIndicatorStream(CandleStream candle,long period) : base(candle,period) { }
        public static string generateID(TimeSpan p) { return "EMA " + p; }
        public override int getLevel()
        {
            return base.getLevel()+1;
        }
        
        public override void Update(TradeTypes.QuoteData qd)
        {
            if (values.Count < Period)
            {
                base.Update(qd);
                return;
            }
            
            double value = candleStream.getByIndexFromEnd(0).Close;
            double alpha = 2 / (Period * Interval.TotalDays + 1);
            double prevema = isNewInterval(qd) ? values.Last().Value : values[values.Count - 2].Value;
            double emai = alpha * (value - prevema) + prevema;
            addValue(qd, emai);
        }
        
        public override string getID()
        {
            return generateID(Period);
        }
    }
}
